//
//  TCM_AuctionDetailCharView.swift
//  TCM_TradingProject
//
//  Created by xiaofeiZhang on 2018/8/30.
//  Copyright © 2018年 lvchunhai. All rights reserved.
//

import UIKit
import Charts
class TCM_AuctionDetailChartView: UIView, ChartViewDelegate {
    let months = ["2016/01", "2016/02", "2016/03",
                  "2016/04", "2016/05", "2016/06",
                  "2016/07", "2016/08", "2016/09",
                  "2016/10", "2016/11", "2016/12",
                  "2017/01", "2017/02", "2017/03",
                  "2017/04", "2017/05", "2017/06",
                  "2017/07", "2017/08", "2017/09",
                  "2017/10", "2017/11", "2017/12",
                  "2018/01", "2018/02", "2018/03",
                  "2018/04", "2018/05", "2018/06",
                  "2018/07", "2018/08", "2018/09",
                  "2018/10", "2018/11", "2018/12",]
    
    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var chartView: LineChartView!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        self.initialChartView()
    }
    
    fileprivate func initialChartView() {
        chartView.delegate = self
        
        chartView.chartDescription?.enabled = false
        chartView.dragEnabled = true
        chartView.setScaleEnabled(true)
        chartView.pinchZoomEnabled = true
        
        let xAxis = chartView.xAxis
        xAxis.labelFont = .systemFont(ofSize: 10)
        xAxis.labelTextColor = UIColor(red: 156/255, green: 156/255, blue: 156/255, alpha: 1)
        xAxis.labelPosition = .bottom
        xAxis.drawAxisLineEnabled = false
        xAxis.valueFormatter = self
        
        let leftAxis = chartView.leftAxis
        leftAxis.labelFont = .systemFont(ofSize: 10)
        leftAxis.labelTextColor = UIColor(red: 156/255, green: 156/255, blue: 156/255, alpha: 1)
        leftAxis.axisMaximum = 120
        leftAxis.axisMinimum = 30
        leftAxis.drawGridLinesEnabled = true
        leftAxis.granularityEnabled = true
        
        self.updateChartData()
        chartView.xAxis.drawGridLinesEnabled = false
        chartView.leftAxis.axisLineColor = UIColor.clear
        chartView.leftAxis.gridColor = UIColor(red: 105/255, green: 60/255, blue: 38/255, alpha: 0.2)
        //        chartView.leftAxis.zeroLineDashLengths = [10, 10]
        chartView.leftAxis.zeroLineColor = UIColor(red: 105/255, green: 60/255, blue: 38/255, alpha: 0.2)

        chartView.setVisibleXRangeMaximum(5)
        chartView.rightAxis.enabled = false
        chartView.legend.enabled = false
        
        chartView.animate(xAxisDuration: 0.35)
    }
    
    fileprivate func updateChartData() {

        self.setDataCount(Int(36), range: UInt32(60))
    }
    
    fileprivate func setDataCount(_ count: Int, range: UInt32) {
        let yVals1 = (0..<count).map { (i) -> ChartDataEntry in
            let val = Double(arc4random_uniform(range) + 30)
            return ChartDataEntry(x: Double(i), y: val)
        }
        
        let set1 = LineChartDataSet(values: yVals1, label: "")
        
        set1.axisDependency = .left
        set1.setColor(UIColor(red: 105/255, green: 60/255, blue: 38/255, alpha: 0.7))
        set1.setCircleColor(UIColor(red: 105/255, green: 60/255, blue: 38/255, alpha: 1))
        set1.lineWidth = 1
        set1.circleRadius = 3
        set1.highlightColor = UIColor(red: 244/255, green: 117/255, blue: 117/255, alpha: 1)
        set1.drawCircleHoleEnabled = false
        set1.drawFilledEnabled = true
        let gradientColors = [ChartColorTemplates.colorFromString("#33FFFFFF").cgColor,
                              ChartColorTemplates.colorFromString("#FF693C26").cgColor]
        let gradient = CGGradient(colorsSpace: nil, colors: gradientColors as CFArray, locations: nil)!
        set1.fillAlpha = 0.7
        set1.fill = Fill(linearGradient: gradient, angle: 90)
//        set1.formSize = 0.0
        set1.highlightLineWidth = 0;//十字线宽度

        let data = LineChartData(dataSets: [set1])
        data.setValueTextColor(.clear)
        data.setValueFont(.systemFont(ofSize: 9))
        
        chartView.data = data
    }
    
    func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight) {
//        super.chartValueSelected(chartView, entry: entry, highlight: highlight)
        
        self.chartView.centerViewToAnimated(xValue: entry.x, yValue: entry.y,
                                            axis: self.chartView.data!.getDataSetByIndex(highlight.dataSetIndex).axisDependency,
                                            duration: 1)
    }
    
}

extension TCM_AuctionDetailChartView: IAxisValueFormatter {
    func stringForValue(_ value: Double, axis: AxisBase?) -> String {
        return months[Int(value) % months.count]
    }
}
